package com.cardone.platform.configuration.dao;

import java.util.List;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.configuration.dto.DictionaryItemDto;

/**
 * 字典项
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class DictionaryItemJdbcDao implements DictionaryItemDao {
	@Override
	public int deleteByIds(final DictionaryItemDto deleteDictionaryItem) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteDictionaryItem.getAttrs())) {
			model.putAll(deleteDictionaryItem.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteDictionaryItem.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final DictionaryItemDto findDictionaryItem) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findDictionaryItem, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findDictionaryItem, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findDictionaryItem.getAttrs())) {
			model.putAll(findDictionaryItem.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final DictionaryItemDto findDictionaryItem) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findDictionaryItem);

		final Model model = ModelUtils.newModel(new ModelArgs(findDictionaryItem, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findDictionaryItem.getAttrs())) {
			model.putAll(findDictionaryItem.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final DictionaryItemDto findListDictionaryItem) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListDictionaryItem).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListDictionaryItem.getAttrs())) {
			model.putAll(findListDictionaryItem.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final DictionaryItemDto insertDictionaryItem) {
		if (StringUtils.isBlank(insertDictionaryItem.getId())) {
			insertDictionaryItem.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertDictionaryItem));

		if (MapUtils.isNotEmpty(insertDictionaryItem.getAttrs())) {
			model.putAll(insertDictionaryItem.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final DictionaryItemDto paginationDictionaryItem) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationDictionaryItem).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationDictionaryItem.getAttrs())) {
			model.putAll(paginationDictionaryItem.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationDictionaryItem), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final DictionaryItemDto readDictionaryItem) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readDictionaryItem, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readDictionaryItem.getAttrs())) {
			model.putAll(readDictionaryItem.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readDictionaryItem);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readDictionaryItem, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final DictionaryItemDto saveDictionaryItem) {
		final P oldDictionaryItem = this.findByCodeOrId(mappedClass, saveDictionaryItem);

		if (oldDictionaryItem == null) {
			this.insertByCode(saveDictionaryItem);

			return this.findByCodeOrId(mappedClass, saveDictionaryItem);
		}

		final int updateCount = this.updateByCode(saveDictionaryItem, oldDictionaryItem);

		if (updateCount < 1) {
			return oldDictionaryItem;
		}

		return this.findByCodeOrId(mappedClass, saveDictionaryItem);
	}

	@Override
	public int updateByCode(final DictionaryItemDto updateDictionaryItem) {
		final DictionaryItemDto oldDictionaryItem = this.findByCodeOrId(DictionaryItemDto.class, updateDictionaryItem);

		return this.updateByCode(updateDictionaryItem, oldDictionaryItem);
	}

	private <P> int updateByCode(final DictionaryItemDto updateDictionaryItem, final P oldDictionaryItem) {
		if (oldDictionaryItem == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateDictionaryItem);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateDictionaryItem, oldDictionaryItem, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldDictionaryItem, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldDictionaryItem, updateDictionaryItem, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateDictionaryItem, updateProperties), new ModelArgs(updateDictionaryItem, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateDictionaryItem.getAttrs())) {
			model.putAll(updateDictionaryItem.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}